﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace TestForCH341Lib
{
    class Program
    {
        [DllImport("CH341Lib.dll", EntryPoint = "CH341_OpenDivce", CallingConvention = CallingConvention.Cdecl)]
        private static extern int CH341_OpenDivce(ulong iIndex);

        [DllImport("CH341Lib.dll", EntryPoint = "CH341_Set_D5_D0_High", CallingConvention = CallingConvention.Cdecl)]
        private static extern int CH341_Set_D5_D0_High(ulong iIndex);

        [DllImport("CH341Lib.dll", EntryPoint = "CH341_Set_D5_D0_Low", CallingConvention = CallingConvention.Cdecl)]
        private static extern int CH341_Set_D5_D0_Low(ulong iIndex);

        [DllImport("CH341Lib.dll", EntryPoint = "CH341_CloseDevice", CallingConvention = CallingConvention.Cdecl)]
        private static extern void CH341_CloseDevice(ulong iIndex);

        static void Main(string[] args)
        {
            ulong iIndex = 0;
            //CH341_CloseDevice(iIndex);
            int ret = CH341_OpenDivce(iIndex);
            if (ret != 0)
            {
                Console.WriteLine("CH341打开失败");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("CH341打开成功");

            Console.WriteLine("你可以用万用表测试CH341的GPIO_D5_D0电压的高低");
            ret = CH341_Set_D5_D0_Low(iIndex);
            if (ret != 0)
            {
                Console.WriteLine("CH341_D5_D0低电平失败");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("CH341_D5_D0低电平成功");

            Console.WriteLine("请按任意键切换电平");
            Console.ReadKey();
            ret = CH341_Set_D5_D0_High(iIndex);
            if (ret != 0)
            {
                Console.WriteLine("CH341_D5_D0高电平失败");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("CH341_D5_D0高电平成功");

            Console.WriteLine("请按任意键切换电平");
            Console.ReadKey();
            ret = CH341_Set_D5_D0_Low(iIndex);
            if (ret != 0)
            {
                Console.WriteLine("CH341_D5_D0低电平失败");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("CH341_D5_D0低电平成功");

            Console.WriteLine("请按任意键切换电平");
            Console.ReadKey();
            ret = CH341_Set_D5_D0_High(iIndex);

            if (ret != 0)
            {
                Console.WriteLine("CH341_D5_D0高电平失败");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("CH341_D5_D0高电平成功");

            Console.WriteLine("请按任意键结束测试");
            Console.ReadKey();
            CH341_CloseDevice(iIndex);
            Console.WriteLine("CH341关闭");

            Console.WriteLine("请按任意键退出程序");
            Console.ReadKey();
        }
    }
}
